package Form

import (
	"fmt"
	"github.com/lxn/walk"
	"github.com/lxn/walk/declarative"
)

/*
type CustomLayout struct {
}

// 实现 declarative.Layout 接口的 Create 方法
func (cl *CustomLayout) Create() (walk.Layout, error) {
	return cl, nil
}

// 实现 walk.Layout 接口的 CreateLayoutItem 方法
func (cl *CustomLayout) CreateLayoutItem(parent walk.Container) walk.LayoutItem {
	return nil // 返回 nil 表示不使用额外的布局项
}
func (cl *CustomLayout) Container() walk.Container {
	return nil // 如果没有父容器，可以返回 nil
}

func (cl *CustomLayout) Layout(container walk.Container) {
	// 自定义布局逻辑
	x, y := 20, 20
	for i := 0; i < container.Children().Len(); i++ { // 使用 Len 和 At 遍历控件
		child := container.Children().At(i)
		if widget, ok := child.(walk.Widget); ok {
			widget.SetBounds(walk.Rectangle{X: x, Y: y, Width: 200, Height: 30})
			y += 40 // 每个控件之间的间隔
		}
	}
}

// 实现 walk.Layout 接口的 SizeHint 方法
func (cl *CustomLayout) SizeHint() walk.Size {
	return walk.Size{Width: 240, Height: 100} // 设置整体大小提示
}*/

// 假设这是walk包中的一些基本类型定义
type Container struct {
	// 定义容器的相关属性
}

/*
	type Margins struct {
		Top    int
		Right  int
		Bottom int
		Left   int
	}

	type LayoutContext struct {
		// 定义布局上下文的相关属性
	}

	type ContainerLayoutItem struct {
		// 定义布局项的相关属性
	}

	type LayoutBase struct {
		container Container
		margins   Margins
		spacing   int
	}

	func (lb *LayoutBase) Container() Container {
		return lb.container
	}

	func (lb *LayoutBase) SetContainer(value Container) {
		lb.container = value
	}

	func (lb *LayoutBase) Margins() Margins {
		return lb.margins
	}

	func (lb *LayoutBase) SetMargins(value Margins) error {
		if value.Top < 0 || value.Right < 0 || value.Bottom < 0 || value.Left < 0 {
			return fmt.Errorf("margins cannot be negative")
		}
		lb.margins = value
		return nil
	}

	func (lb *LayoutBase) Spacing() int {
		return lb.spacing
	}

	func (lb *LayoutBase) SetSpacing(value int) error {
		if value < 0 {
			return fmt.Errorf("spacing cannot be negative")
		}
		lb.spacing = value
		return nil
	}

	func (lb *LayoutBase) CreateLayoutItem(ctx *LayoutContext) ContainerLayoutItem {
		// 这里可以根据ctx创建具体的ContainerLayoutItem
		item := ContainerLayoutItem{}
		// 初始化item...
		return item
	}

	func (lb *LayoutBase) asLayoutBase() *LayoutBase {
		return lb
	}
*/
type CustomLayout struct {
	*walk.LayoutBase
}

func NewCustomLayout() *CustomLayout {
	return &CustomLayout{
		LayoutBase: &walk.LayoutBase{},
	}
}

// 实现 walk.Layout 接口的方法
func (cl *CustomLayout) LayoutFlags() walk.LayoutFlags {
	return walk.ShrinkableHorz | walk.GrowableVert
}

func (cl *CustomLayout) MinSize() walk.Size {
	return walk.Size{Width: 100, Height: 50}
}

func (cl *CustomLayout) Update(widgets []walk.Widget) {
	for _, widget := range widgets {
		widget.SetBounds(walk.Rectangle{X: 0, Y: 0, Width: 100, Height: 50})
	}
}

// CreateLayoutItem 补充缺失的方法
/*func (cl *CustomLayout) CreateLayoutItem(ctx *walk.LayoutContext) walk.LayoutItem {
	// 创建并返回一个自定义的 LayoutItem
	return walk.NewGreedyLayoutItem()
}*/
// CreateLayoutItem 补充缺失的方法
func (cl *CustomLayout) CreateLayoutItem(ctx *walk.LayoutContext) walk.ContainerLayoutItem {
	// 创建一个 GreedyLayoutItem，并将其转换为 ContainerLayoutItem
	layoutItem := walk.NewGreedyLayoutItem()
	containerLayoutItem, ok := layoutItem.(walk.ContainerLayoutItem)
	if !ok {
		panic("failed to convert LayoutItem to ContainerLayoutItem")
	}
	return containerLayoutItem
}

/*
func (cl *CustomLayout) CreateLayoutItem(ctx *walk.LayoutContext) walk.ContainerLayoutItem {
	return walk.NewGreedyLayoutItem().Parent()
}*/

// Create 实现 declarative.Layout 接口的 Create 方法
func (cl *CustomLayout) Create() (walk.Layout, error) {
	return cl, nil
}

/*
	func (cl *CustomLayout) Create(ctx *walk.Form) (Container, error) {
		// 创建并返回一个Container实例
		return Container{}, nil
	}
*/
func (cl *CustomLayout) asLayoutBase() *walk.LayoutBase {
	return cl.LayoutBase
}

// FreeLayout 自定义自由布局
type FreeLayout struct{}

// CreateLayoutItem 创建布局项
func (fl *FreeLayout) CreateLayoutItem(ctx *walk.LayoutContext) walk.LayoutItem {
	return new(FreeLayoutItem)
}

// FreeLayoutItem 自定义布局项
type FreeLayoutItem struct {
	walk.LayoutItemBase
}

// LayoutFlags 返回布局标志
func (fli *FreeLayoutItem) LayoutFlags() walk.LayoutFlags {
	return walk.ShrinkableHorz | walk.ShrinkableVert | walk.GrowableHorz | walk.GrowableVert
}

// MinSize 返回最小尺寸
func (fli *FreeLayoutItem) MinSize() walk.Size {
	return walk.Size{Width: 100, Height: 100} // 默认最小尺寸
}

// Update 更新布局
func (fli *FreeLayoutItem) Update(widgets []walk.Widget) {
	for _, widget := range widgets {
		bounds := widget.Bounds()

		// 根据窗口大小动态调整控件位置
		newX := bounds.X + 10 // 示例：水平方向偏移
		newY := bounds.Y + 10 // 示例：垂直方向偏移

		widget.SetBounds(walk.Rectangle{X: newX, Y: newY, Width: bounds.Width, Height: bounds.Height})
	}
}

func (fl FreeLayout) Create() (walk.Layout, error) {
	l := walk.NewGridLayout()

	return l, nil
}

type FormModule2 struct{}

func (m *FormModule2) DemoSelectForm() {
	// 创建主窗口
	mainWindow, err := walk.NewMainWindow()
	if err != nil {
		panic(err)
	}

	// 设置窗口属性
	mainWindow.SetTitle("自由布局示例")
	mainWindow.SetSize(walk.Size{Width: 400, Height: 300})

	// 使用一个简单的占位布局（如 VBoxLayout）
	layout := walk.NewVBoxLayout() // 创建垂直布局
	if layout == nil {
		fmt.Println("无法创建布局")
		return
	}
	mainWindow.SetLayout(layout) // 设置布局

	// 创建 ComboBox
	comboBox, err := walk.NewComboBox(mainWindow)
	if err != nil {
		panic(err)
	}
	comboBox.SetBounds(walk.Rectangle{X: 50, Y: 50, Width: 150, Height: 25})
	//comboBox.AddItems([]string{"选项 1", "选项 2", "选项 3"})
	comboBox.SetModel([]string{"选项 1", "选项 2", "选项 3"})
	// 创建 PushButton
	button, err := walk.NewPushButton(mainWindow)
	if err != nil {
		panic(err)
	}
	button.SetText("按钮")
	button.SetBounds(walk.Rectangle{X: 250, Y: 150, Width: 100, Height: 30})

	// 显示窗口
	mainWindow.Show()

	// 启动消息循环
	mainWindow.Run()
}
func (m *FormModule) DemoSelectForm() {

	module := &FormModule2{}
	module.DemoSelectForm()
	/*var comboBox *walk.ComboBox

	// 定义窗口
	declarative.MainWindow{
		Title:   "自定义布局示例",
		MinSize: declarative.Size{300, 100},
		Layout:  NewCustomLayout(), // 使用自定义布局
		Children: []declarative.Widget{
			declarative.ComboBox{
				AssignTo: &comboBox,
				Model:    []string{"选项 1", "选项 2", "选项 3"},
			},
			declarative.PushButton{
				Text: "确定",
				OnClicked: func() {
					selected := comboBox.Text()
					walk.MsgBox(nil, "选中项", fmt.Sprintf("你选择了: %s", selected), walk.MsgBoxIconInformation)
				},
			},
		},
	}.Run()*/
}
func T1() {

	type Foo struct {
		Bar string
		Baz int
	}
	foo := &Foo{"b", 0}

	declarative.MainWindow{
		Title:   "Walk RadioButton Example",
		MinSize: declarative.Size{320, 240},
		Layout:  declarative.VBox{},
		DataBinder: declarative.DataBinder{
			DataSource: foo,
			AutoSubmit: true,
			OnSubmitted: func() {
				fmt.Println(foo)
			},
		},
		Children: []declarative.Widget{
			// RadioButtonGroup is needed for data binding only.
			declarative.RadioButtonGroup{
				DataMember: "Bar",
				Buttons: []declarative.RadioButton{
					declarative.RadioButton{
						Name:  "aRB",
						Text:  "A",
						Value: "a",
					},
					declarative.RadioButton{
						Name:  "bRB",
						Text:  "B",
						Value: "b",
					},
					declarative.RadioButton{
						Name:  "cRB",
						Text:  "C",
						Value: "c",
					},
				},
			},
			declarative.Label{
				Text:    "A",
				Enabled: declarative.Bind("aRB.Checked"),
			},
			declarative.Label{
				Text:    "B",
				Enabled: declarative.Bind("bRB.Checked"),
			},
			declarative.Label{
				Text:    "C",
				Enabled: declarative.Bind("cRB.Checked"),
			},
			declarative.RadioButtonGroup{
				DataMember: "Baz",
				Buttons: []declarative.RadioButton{
					declarative.RadioButton{
						Name:  "oneRB",
						Text:  "1",
						Value: 1,
					},
					declarative.RadioButton{
						Name:  "twoRB",
						Text:  "2",
						Value: 2,
					},
					declarative.RadioButton{
						Name:  "threeRB",
						Text:  "3",
						Value: 3,
					},
				},
			},
			declarative.Label{
				Text:    "1",
				Enabled: declarative.Bind("oneRB.Checked"),
			},
			declarative.Label{
				Text:    "2",
				Enabled: declarative.Bind("twoRB.Checked"),
			},
			declarative.Label{
				Text:    "3",
				Enabled: declarative.Bind("threeRB.Checked"),
			},
		},
	}.Run()
}
func T2() {
	// 定义窗口
	// 创建一个主窗口
	mw, _ := walk.NewMainWindow()
	mw.SetTitle("ComboBox 示例")
	mw.SetSize(walk.Size{Width: loginGUIWidth, Height: loginGUIHeight})
	//mw.SetLayout(walk.Layout{})
	// 创建一个组合框
	comboBox, _ := walk.NewComboBox(mw)
	comboBox.SetModel([]string{"选项 1", "选项 2", "选项 3"})
	comboBox.SetBounds(walk.Rectangle{X: 20, Y: 20, Width: 200, Height: 30}) // 设置位置和大小

	// 创建一个按钮
	button, _ := walk.NewPushButton(mw)
	button.SetText("确定")
	button.SetBounds(walk.Rectangle{X: 20, Y: 60, Width: 100, Height: 30}) // 设置位置和大小

	// 按钮点击事件
	button.Clicked().Attach(func() {
		selected := comboBox.Text()
		walk.MsgBox(mw, "选中项", fmt.Sprintf("你选择了: %s", selected), walk.MsgBoxIconInformation)
	})
	// 显示窗口
	mw.Show()
	mw.Run()

}
